package com.anysoft.fund;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.anysoft.R;
import com.anysoft.bean.QueryFundListBean;
import com.anysoft.web.FundDetailActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wwq on 2017/1/12.
 */

public class ScrollAdapter extends BaseAdapter {

    private int res;
    private int[] to;
    private Context context;
    private ArrayList<QueryFundListBean.DataBean> itemList;
    private int currentType;
    private FundListAllActivity fundListAllActivity;
    private List<CHScrollView_Others> mChScrollView_others;
    private ListView listView;

    public ScrollAdapter(Context context, int resource, int[] to,
                         ArrayList<QueryFundListBean.DataBean> itemList, int currentType,
                         FundListAllActivity fundListAllActivity, List<CHScrollView_Others> mChScrollView_others, ListView listView) {
        this.context = context;
        this.res = resource;
        this.to = to;
        this.itemList = itemList;
        this.currentType = currentType;
        this.fundListAllActivity = fundListAllActivity;
        this.mChScrollView_others = mChScrollView_others;
        this.listView = listView;
    }

    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public Object getItem(int position) {
        return itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            v = LayoutInflater.from(context).inflate(res, null);
            CHScrollView_Others chScrollView_others = (CHScrollView_Others) v.findViewById(R.id.item_chscroll_scroll);

            chScrollView_others.setType(currentType);
            fundListAllActivity.addHViews(chScrollView_others, mChScrollView_others, listView);

            View[] views = new View[to.length];
            for (int i = 0; i < to.length; i++) {
                View tv = v.findViewById(to[i]);
                views[i] = tv;
            }

            v.setTag(views);
        }
        View[] holders = (View[]) v.getTag();
        int len = holders.length;
        for (int i = 0; i < len; i++) {
            switch (i) {
                case 0:
                    ((TextView) holders[i]).setText(itemList.get(position).getFundname());
                    break;
                case 1:
                    switch (Integer.parseInt(itemList.get(position).getGroupNum())) {
                        case 1:
                            // initTextview(((TextView) holders[i]),
                            // itemList.get(position).getPernetvalue());
                            ((TextView) holders[i]).setText(itemList.get(position).getPernetvalue());
                            break;
                        case 2:
                            // initTextview(((TextView) holders[i]),
                            // itemList.get(position).getHfincomeratio());
                            ((TextView) holders[i]).setText(itemList.get(position).getHfincomeratio());
                            break;
                        default:
                            break;
                    }
                    break;
                case 2:
                    switch (Integer.parseInt(itemList.get(position).getGroupNum())) {
                        case 1:
                            initTextview(((TextView) holders[i]), itemList.get(position).getDayinc());
                            // ((TextView)
                            // holders[i]).setText(itemList.get(position).getDayinc());
                            break;
                        case 2:
                            initTextview(((TextView) holders[i]), itemList.get(position).getIncomeratio());
                            // ((TextView)
                            // holders[i]).setText(itemList.get(position).getIncomeratio());
                            break;
                        default:
                            break;
                    }
                    break;
                case 3:
                    initTextview(((TextView) holders[i]), itemList.get(position).getPeriodWeek());
                    // ((TextView)
                    // holders[i]).setText(itemList.get(position).getPeriodWeek());
                    break;
                case 4:
                    initTextview(((TextView) holders[i]), itemList.get(position).getPeriodMonthOne());
                    // ((TextView)
                    // holders[i]).setText(itemList.get(position).getPeriodMonthOne());
                    break;
                case 5:
                    initTextview(((TextView) holders[i]), itemList.get(position).getPeriodMonthThree());
                    // ((TextView)
                    // holders[i]).setText(itemList.get(position).getPeriodMonthThree());
                    break;
                case 6:
                    initTextview(((TextView) holders[i]), itemList.get(position).getPeriodMonthSix());
                    // ((TextView)
                    // holders[i]).setText(itemList.get(position).getPeriodMonthSix());
                    break;
                case 7:
                    initTextview(((TextView) holders[i]), itemList.get(position).getPeriodSinceYear());
                    // ((TextView)
                    // holders[i]).setText(itemList.get(position).getPeriodSinceYear());
                    break;
                case 8:
                    initTextview(((TextView) holders[i]), itemList.get(position).getPeriodSinceEstablish());
                    // ((TextView)
                    // holders[i]).setText(itemList.get(position).getPeriodSinceEstablish());
                    break;
                case 9:
                    if (!TextUtils.isEmpty(itemList.get(position).getIsNewFund()) &&
                            Integer.parseInt(itemList.get(position).getIsNewFund()) == 1) {
                        holders[i].setVisibility(View.VISIBLE);
                    } else {
                        holders[i].setVisibility(View.GONE);
                    }
                    break;
                case 10:
                    if (!TextUtils.isEmpty(itemList.get(position).getIsRecommend()) &&
                            Integer.parseInt(itemList.get(position).getIsRecommend()) == 1) {
                        holders[i].setVisibility(View.VISIBLE);
                    } else {
                        holders[i].setVisibility(View.GONE);
                    }
                    break;
                case 11:
                    ((TextView) holders[i]).setText(itemList.get(position).getFundcode());
                    break;
                case 12:
                    if (TextUtils.isEmpty(itemList.get(position).getNavdate())) {
                        ((TextView) holders[i]).setText("-");
                    } else {
                        String date = itemList.get(position).getNavdate();
                        if (date.length() == 8) {
                            date = date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8);
                        }
                        ((TextView) holders[i]).setText(date);
                    }
                    break;

                default:
                    break;
            }
            holders[i].setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    gotoDetails(itemList.get(position));
                }
            });
        }
        return v;
    }

    private void gotoDetails(QueryFundListBean.DataBean bean) {
        Intent intent = new Intent().setClass(context, FundDetailActivity.class);
        intent.putExtra("fundcode", bean.getFundcode());
        intent.putExtra("fundname", bean.getFundname());
        intent.putExtra("fundType", bean.getFundtype());
        intent.putExtra("mainTitle", bean.getFundname() + " " + bean.getFundcode());
        intent.putExtra("subTitle", bean.getFundstateDescription());
        context.startActivity(intent);
    }

    private void initTextview(TextView textView, String str) {
        if (TextUtils.isEmpty(str)) {
            textView.setText("--");
        } else {
            if (str.contains("-")) {
                textView.setTextColor(context.getResources().getColor(R.color.black));
                textView.setText(str);
            } else {
                textView.setTextColor(context.getResources().getColor(R.color.gongyin_nomarl_red));
                textView.setText(str);
            }
        }
    }

}
